package com.huanf.media.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanf.domain.entity.MediaFiles;
import com.huanf.domain.entity.MediaProcessHistory;
import com.huanf.media.mapper.MediaFilesMapper;
import com.huanf.media.mapper.MediaProcessHistoryMapper;
import com.huanf.media.mapper.MediaProcessMapper;
import com.huanf.media.service.MediaProcessHistoryService;
import com.huanf.media.service.MediaProcessService;
import com.huanf.domain.entity.MediaProcess;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * (MediaProcess)表服务实现类
 *
 * @author makejava
 * @since 2024-04-15 22:18:38
 */
@Service
@Slf4j
public class MediaProcessServiceImpl extends ServiceImpl<MediaProcessMapper, MediaProcess> implements MediaProcessService {

    @Autowired
    MediaProcessMapper mediaProcessMapper;

    @Autowired
    MediaFilesMapper mediaFilesMapper;

    @Autowired
    MediaProcessHistoryMapper mediaProcessHistoryMapper;

    @Override
    public List<MediaProcess> selectListByShardIndex(int shardTotal, int shardIndex, int count) {
        return mediaProcessMapper.selectListByShardIndex(shardTotal, shardIndex, count);
    }

    @Override
    public boolean startTask(Long id) {
        int i = mediaProcessMapper.startTask(id);
        return i <= 0 ? false : true;
    }

    @Override
    public void saveProcessFinishStatus(Long taskId, String status, String errorMsg, String url, String fileId) {
        //获取要更新的任务信息
        MediaProcess mediaProcess = mediaProcessMapper.selectById(taskId);
        if (mediaProcess == null) {
            log.error("任务不存在，无法更新状态");
            return;
        }
        //如果任务执行失败
        if(status.equals("3")){
            //更新media_file表中的状态
            mediaProcess.setStatus("3");
            mediaProcess.setFailCount(mediaProcess.getFailCount() + 1);
            mediaProcess.setErrormsg(errorMsg);
            //更新数据库
            mediaProcessMapper.updateById(mediaProcess);
            //更高效的更新方式  mediaProcessMapper.update()
            return;
        }
        //===============如果任务执行成功================
        //文件表记录
        MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
        if(mediaFiles == null) {
            log.error("文件记录不存在，fileId: {}", fileId);
            return;
        }
        //更新media_file表中的url
        mediaFiles.setUrl(url);
        mediaFilesMapper.updateById(mediaFiles);
        //跟新mediaProcess表的状态
        mediaProcess.setStatus("2");
        mediaProcess.setFinishDate(LocalDateTime.now());
        mediaProcess.setUrl(url);
        mediaProcessMapper.updateById(mediaProcess);
        //将mediaProcess表记录插入到MediaProcessHistory表中
        MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
        BeanUtils.copyProperties(mediaProcess, mediaProcessHistory);
        mediaProcessHistory.setId(null);
        mediaProcessHistory.setCreateDate(LocalDateTime.now());
        mediaProcessHistoryMapper.insert(mediaProcessHistory);
        //从MediaProcess表中删除该记录
        mediaProcessMapper.deleteById(taskId);
    }
}
